package com.example.day9.controller;

import com.example.day9.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @Author Linyh
 * @Description
 * @Date 2022/7/14 19:55
 **/

@Controller
public class UserController {

    @Autowired
    private User user;

    @RequestMapping({"/", "/index.html"})
    public String index() {
        return "userLogin";
    }

    @PostMapping("/user/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("passwd") String passwd,
                        Model model) {
        if (username.equals(user.getUsername()) && passwd.equals(user.getPasswd())) {
            model.addAttribute("msg", "登录成功");
            return "userLogin";
        } else {
            model.addAttribute("msg", "用户账号或密码错误,请注册");
            return "userRegist";
        }

    }

    @PostMapping("/user/regist")
    public String regist(User user1,Model model) {
        if(user1.getUsername().equals(user.getUsername())){
            model.addAttribute("msg", "该账号已被注册,请重新注册");
            return "userRegist";
        }else {
            model.addAttribute("msg", "用户注册成功,请登录");
            return "userLogin";
        }
    }
}
